﻿using ELab.Core;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace ELab.Serialization.Data
{
    public class UserProvider
    {
        public const string KEY_USER_PROVIDER = "UserProvider";
        private string _fileName = ELab.Settings.ELabSettings.AssemblyDirectory + "\\users.lab";
        public List<User> Users
        {
            get
            {
                List<User> users = null;//if (users != null) return users;
                // Load the customer object from the existing XML file (if any)...
                if (File.Exists(_fileName) == true)
                {
                    // Load the customer object from the XML file using our custom class...
                    var obj = ObjectXMLSerializer<List<User>>.Load(_fileName);
                    if (obj != null)
                    {
                        users = (List<User>)obj;
                        users = users.Select((u) =>
                        {
                            u.ApplicationName = Algorithm.Decode(KEY_USER_PROVIDER, u.ApplicationName);
                            u.Email = Algorithm.Decode(KEY_USER_PROVIDER, u.Email);
                            u.Fullname = Algorithm.Decode(KEY_USER_PROVIDER, u.Fullname);
                            u.IP = Algorithm.Decode(KEY_USER_PROVIDER, u.IP);
                            u.RemotePassword = Algorithm.Decode(KEY_USER_PROVIDER, u.RemotePassword);
                            u.RemoteUser = Algorithm.Decode(KEY_USER_PROVIDER, u.RemoteUser);
                            u.UserName = Algorithm.Decode(KEY_USER_PROVIDER, u.UserName);
                            return u;
                        }).ToList();
                    }
                }
                return users;
            }
        }

        public void Add(params User[] dest)
        {
            dest = dest.Select((u) => { u.Password = Algorithm.SHAPassword(u.Password); return u; }).ToArray();
            var users = this.Users.AddExt(dest);
            Save(users);
        }

        public void Save(List<User> users)
        {
            var userSave = users.Select((u) =>
            {
                u.ApplicationName = Algorithm.Encode(KEY_USER_PROVIDER, u.ApplicationName);
                u.Email = Algorithm.Encode(KEY_USER_PROVIDER, u.Email);
                u.Fullname = Algorithm.Encode(KEY_USER_PROVIDER, u.Fullname);
                u.IP = Algorithm.Encode(KEY_USER_PROVIDER, u.IP);
                u.RemotePassword = Algorithm.Encode(KEY_USER_PROVIDER, u.RemotePassword);
                u.RemoteUser = Algorithm.Encode(KEY_USER_PROVIDER, u.RemoteUser);
                u.UserName = Algorithm.Encode(KEY_USER_PROVIDER, u.UserName);
                return u;
            }).ToList();
            ObjectXMLSerializer<List<User>>.Save(userSave, _fileName);
        }

        public bool Exist(User user)
        {
            return this.Users.Any(x => x.ID == user.ID && x.ApplicationName == user.ApplicationName);
        }

        public void Remove(User userRemove)
        {
            var users = this.Users;
            var userCheck = users.FirstOrDefault(x => x.ID == userRemove.ID && x.ApplicationName == userRemove.ApplicationName);
            users.Remove(userCheck);
            Save(users);
        }

        public void Update(User user)
        {
            var users = this.Users;
            var userCheck = users.FirstOrDefault(x => x.ID == user.ID && x.ApplicationName == user.ApplicationName);
            var userIndex = users.IndexOf(userCheck);
            users[userIndex] = user;
            Save(users);
        }
    }
}
